<template>
  <a-card :bordered="false">
<!--    <div class="table-page-search-wrapper">-->
<!--      <a-form layout="inline">-->
<!--        <a-row :gutter="48">-->
<!--          <a-col :md="8" :sm="24">-->
<!--            <a-form-item label="单位名称">-->
<!--              <a-input v-model="queryParam.companyName" placeholder=""/>-->
<!--            </a-form-item>-->
<!--          </a-col>-->
<!--          <template v-if="advanced">-->
<!--            <a-col :md="8" :sm="24">-->
<!--              <a-form-item label="船名">-->
<!--                <a-input v-model="queryParam.shipName" placeholder=""/>-->
<!--              </a-form-item>-->
<!--            </a-col>-->
<!--          </template>-->
<!--          <a-col :md="!advanced && 8 || 24" :sm="24">-->
<!--            <span class="table-page-search-submitButtons" :style="advanced && { float: 'right', overflow: 'hidden' } || {} ">-->
<!--              <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>-->
<!--              <a-button style="margin-left: 8px" @click="() => queryParam = {}">重置</a-button>-->
<!--              <a @click="toggleAdvanced" style="margin-left: 8px">-->
<!--                {{ advanced ? '收起' : '展开' }}-->
<!--                <a-icon :type="advanced ? 'up' : 'down'"/>-->
<!--              </a>-->
<!--            </span>-->
<!--          </a-col>-->
<!--        </a-row>-->
<!--      </a-form>-->
<!--    </div>-->

    <div class="table-operator">
      <a-button type="primary" icon="plus" @click="$refs.addWork.add()">新增船舶工作记录</a-button>
      <a-button type="dashed" @click="tableOption">{{ optionAlertShow && '关闭' || '开启' }}提示</a-button>
      <a-dropdown v-action:edit v-if="selectedRowKeys.length > 0">
        <a-menu slot="overlay">
          <a-menu-item key="remove">
            <remove-user ref="removeShip" :shipIds="selectedRowKeys.join(',')" @ok="handleOk"/>
          </a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px">
          批量操作 <a-icon type="down" />
        </a-button>
      </a-dropdown>
    </div>

    <s-table
      ref="table"
      size="default"
      rowKey="key"
      :columns="columns"
      :data="loadData"
      :alert="options.alert"
      :rowSelection="options.rowSelection"
      showPagination="auto"
    >
      <!--      插槽 带数据的插槽-->
      <span slot="action" slot-scope="text, record">
        <template>
          <remove-work ref="removeWork" :workIds="String(record.workId)" @ok="handleOk"/>
        </template>
      </span>
    </s-table>
    <!--    <add-ship ref="addShip" :roles="roles" @ok="handleOk" />-->
    <!--    <edit-ship ref="editShip" :roles="roles" @ok="handleOk"/>-->

    <add-work ref="addWork" :ships="ships" :workTypes="workTypes" @ok="handleOk" />
  </a-card>
</template>

<script>
// import moment from 'moment'
import { STable } from '@/components'
import AddWork from './modules/work/AddWork'
import RemoveWork from './modules/work/RemoveWork'
import EditShip from './modules/ship/EditShip'
import RemoveShip from './modules/ship/RemoveShip'
import { queryShipWork, queryWorkType } from '@/api/ship/work'
import { queryShipInPort } from '@/api/ship/ship'

// 用于处理过滤器动态获取数据
// let that
export default {
  name: 'WorkRegister',
  components: {
    STable,
    AddWork,
    RemoveWork,
    EditShip,
    RemoveShip
  },
  data () {
    return {
      // 高级搜索 展开/关闭
      advanced: false,
      // 查询参数
      queryParam: {},
      // 角色对象
      roles: [],
      ships: {},
      workTypes: {},
      // 表头
      columns: [
        {
          title: '单位名称',
          dataIndex: 'companyName',
          align: 'center'
        },
        {
          title: '业务员人数',
          dataIndex: 'peopleNumber',
          align: 'center'
        },
        {
          title: '联系方式',
          dataIndex: 'phone',
          align: 'center'
        },
        {
          title: '船名',
          dataIndex: 'shipName',
          align: 'center'
        },
        {
          title: '工作类型',
          dataIndex: 'workName',
          align: 'center'
        },
        {
          title: '工作详情',
          dataIndex: 'workDetail',
          align: 'center'
        },
        // {
        //   title: '新建时间',
        //   dataIndex: 'gmtCreate',
        //   align: 'center',
        //   customRender: (text) => moment(text).format('YYYY-MM-DD')
        // },
        {
          title: '操作',
          dataIndex: 'action',
          width: '150px',
          align: 'center',
          scopedSlots: { customRender: 'action' }
        }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        // 统一处理查询参数，便于后端表格DTO用固定参数接收
        const queryJson = { 'queryParam': JSON.stringify(this.queryParam) }
        queryWorkType(Object.assign(parameter, queryJson)).then(res => {
          this.workTypes = res.result
        })
        return queryShipWork(Object.assign(parameter, queryJson))
          .then(res => {
            console.log(res.result)
            return res.result
          })
      },

      selectedRowKeys: [],
      selectedRows: [],

      // custom table alert & rowSelection
      options: {
        alert: { show: true, clear: () => { this.selectedRowKeys = [] } },
        rowSelection: {
          selectedRowKeys: this.selectedRowKeys,
          onChange: this.onSelectChange
        }
      },
      optionAlertShow: false
    }
  },
  filters: {
    // roleTextFilter (roleId) {
    //   let roleName = ''
    //   for (const role of that.roles) {
    //     if (parseInt(role.id) === parseInt(roleId)) {
    //       roleName = role.name
    //       break
    //     }
    //   }
    //   return roleName
    // }
  },
  beforeCreate() {
    // that = this
  },
  created() {
    this.tableOption()
    this.getShip()
  },
  methods: {

    getShip() {
      queryShipInPort().then(res => {
        this.ships = res.result
      })
    },
    tableOption () {
      if (this.optionAlertShow) {
        this.options = {
          alert: { show: true, clear: () => { this.selectedRowKeys = [] } },
          rowSelection: {
            selectedRowKeys: this.selectedRowKeys,
            onChange: this.onSelectChange
          }
        }
        this.optionAlertShow = true
      } else {
        this.options = {
          alert: false,
          rowSelection: null
        }
        this.optionAlertShow = false
      }
    },
    handleRemove (record) {
      this.$refs.removeWork.edit(record)
    },
    handleOk () {
      this.$refs.table.clearSelected()
      this.$refs.table.refresh(false)
    },
    onSelectChange (selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    },
    toggleAdvanced () {
      this.advanced = !this.advanced
    }
  }
}
</script>
